package com.jstyle.jclifexd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.ActivitvtiesAdapter;
import com.jstyle.jclifexd.model.Activitities;
import com.jstyle.jclifexd.model.ActivitvtiesData;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.QueryUserStatemsg;
import com.jstyle.jclifexd.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动
 */
public class TeamMembersActivity extends BaseActivity{
    @BindView(R.id.activity_RecyclerView)
    RecyclerView activity_RecyclerView;
    ActivitvtiesAdapter activitvtiesAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        ButterKnife.bind(this);
        activity_RecyclerView.setLayoutManager(new LinearLayoutManager(this));
        activitvtiesAdapter=new ActivitvtiesAdapter(this);
        activity_RecyclerView.setAdapter(activitvtiesAdapter);
        activitvtiesAdapter.setOnItemClickListener(new ActivitvtiesAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Activitities aa, int position) {
                if(Utils.checkNetWork(TeamMembersActivity.this)){
                   switch (aa.getType()){
                       case "pk":
                           QueryUserState(aa);
                           break;
                           default:
                               //进入排名界面

                               break;

                    }

                }else{
                    showToast(TeamMembersActivity.this.getString(R.string.Network_not_availa));
                } }});
        Querydata();
    }


    private void QueryUserState(final Activitities aa){
        Map<String,Object> map=new HashMap<>();
        map.put("uid",NetWorkUtil.getUserId());
        NetWorkUtil.getInstance().getJstyleApi().getqueryUserState(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<QueryUserStatemsg>() {
            @Override
            public void onSubscribe(Disposable d) {}
            @Override
            public void onNext(QueryUserStatemsg activitvtiesData) {
                if(activitvtiesData.getData().getState().equals("no")){
                    Intent intent=new Intent(TeamMembersActivity.this,TeamPKActivity.class);
                    intent.putExtra("activitiesActivity",aa);
                    startActivity(intent);
                }
            }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });

    }

    private void Querydata(){
        Map<String, Object> map = new HashMap<>();
        map.put("language",  Utils.isZh(TeamMembersActivity.this)?"zh-CN":"en");
        NetWorkUtil.getInstance().getJstyleApi().getActivity(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ActivitvtiesData>() {
            @Override
            public void onSubscribe(Disposable d) {}
            @Override
            public void onNext(ActivitvtiesData activitvtiesData) {
               List<Activitities> data=new ArrayList<>();
                for (int i=0;i<activitvtiesData.getData().size();i++){
                 if(Utils.isZh(TeamMembersActivity.this)){
                    if(activitvtiesData.getData().get(i).getLanguage().contains("zh")){
                        data.add(activitvtiesData.getData().get(i));
                    }
                }else{
                if(activitvtiesData.getData().get(i).getLanguage().contains("en")){
                    data.add(activitvtiesData.getData().get(i));
                } }
                }
                activitvtiesAdapter.SetData(data);
            }
            @Override
            public void onError(Throwable e) { }
            @Override
            public void onComplete() { }
        });

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
